package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.bean.PayForSuccessBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.LogUtils;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPager;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

public class PayForSuccessActivity extends BaseActivity {

    @BindView(R.id.tv_payfor_money_activiyt_for_success)
    TextView mTvPayforMoneyActiviytForSuccess;
    @BindView(R.id.tv_payfor_order_activiyt_for_success)
    TextView mTvPayforOrderActiviytForSuccess;
    @BindView(R.id.tv_house_name_activiyt_for_success)
    TextView mTvHouseNameActiviytForSuccess;
    @BindView(R.id.tv_goshop_activity_pay_for_success)
    TextView mTvGoshopActivityPayForSuccess;
    @BindView(R.id.tv_salehouse_step_activity_pay_for_success)
    TextView mTvSalehouseStepActivityPayForSuccess;
    private LoadingPager mLoadingPager;
    private String mRoomId;
    private String mMoneyStr;
    private String mOrderId;
    private String mPayStyle;
    private String purchaseId;
    @Override
    protected void initView() {

        mLoadingPager = new LoadingPager(this) {
            @Override
            protected void close() {
                finish();
            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(PayForSuccessActivity.this,R.layout.activity_pay_for_success);
            }

            @Override
            protected void reLoading() {
                retry();
            }
        };
        //setContentView(R.layout.activity_pay_for_success);
        setContentView(mLoadingPager);
    }

    /**
     * 重试
     */
    private void retry() {
        Observable observable = ApiUtils.getApi().payforSuccess(mRoomId, mMoneyStr, mOrderId,mPayStyle);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<PayForSuccessBean>(this) {


            @Override
            protected void _onNext(StatusCode<PayForSuccessBean> t) {

                PayForSuccessBean payForSuccessBean = t.getData();
                if (t.isFlag()&&payForSuccessBean!=null){
                    mLoadingPager.showPagerView(Contans.STATE_SUCCEED);

                    mTvPayforMoneyActiviytForSuccess.setText("定金 :"+payForSuccessBean.getDeposit()+"元");
                    mTvPayforOrderActiviytForSuccess.setText("订单 :"+payForSuccessBean.getOrderId()+"");
                    mTvHouseNameActiviytForSuccess.setText("房型 :"+payForSuccessBean.getFullname()+"");

                    purchaseId = payForSuccessBean.getPurchaseId();
                }else {
                    mLoadingPager.showPagerView(Contans.STATE_EMPTY);
                    ToastUtils.makeText(t.getDetailMsg());
                }
            }

            @Override
            protected void _onError(String message) {
             mLoadingPager.showPagerView(Contans.STATE_ERROR);
                ToastUtils.makeText(message);



            }
        },"",lifecycleSubject,false,true);




    }


    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
       //  String   mData = intent.getStringExtra("_wxapi_payresp_extdata");
         String   mData = intent.getStringExtra(Contans.INTENT_DATA);
        LogUtils.i(TAG,"data::"+ mData);
        if (!StringUtils.isEmpty(mData)){
            String[] split = mData.split("##");
            if (split!=null&&split.length==4){
                mRoomId = split[0];
                mMoneyStr = split[1];
                mOrderId = split[2];
                mPayStyle = split[3];
            }

        }

        retry();

    }

    @OnClick(R.id.tv_goshop_activity_pay_for_success)
    public void onMTvGoshopActivityPayForSuccessClicked() {

      //  ToastUtils.makeText("暂未开放，敬请稍等...");
/*if (purchaseId!=null){
    BuyHouseStepActivity.startActivity(this,purchaseId);
}*/

MainActivity.goGoodsShop(this,1);
    }

    @OnClick(R.id.tv_salehouse_step_activity_pay_for_success)
    public void onMTvSalehouseStepActivityPayForSuccessClicked() {

       // ToastUtils.makeText("暂未开放，敬请稍等...");
        if (purchaseId!=null){
            BuyHouseStepActivity.startActivity(this,purchaseId, BaseApplication.sUserInfoBean.username);
            finish();
        }
    }

    public static void startActivity(Context context, String data) {

        Intent intent = new Intent(context, PayForSuccessActivity.class);

        intent.putExtra(Contans.INTENT_DATA, data);
        context.startActivity(intent);
    }
}
